//
//  MyNetWork.swift
//  BaseProject
//
//  Created by guolinsong on 2018/7/5.
//  Copyright © 2018年 guolin song. All rights reserved.
//

import UIKit
import Foundation

import Alamofire

var host:String{
    #if DEBUG
    return BASE_URL_DEBUG
    #else
    return BASE_URL
    #endif
}

enum MethodType {
    case GET
    case POST
}
enum RequestType:Int{
    case FORM = 0
    case JSON
}



class MyNetWork: NSObject {
    
    //类方法
     class func requestAction(type: MethodType = .POST,url:String,parameters:Parameters? = nil, finished: @escaping (_ response: String) -> ()){
        
        
        let fullUrl = "\(host)+\(url)"
        let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
        
//        var encodingMethod:ParameterEncoding?
//        if tempRequestType == RequestType.JSON {
//            encodingMethod = JSONEncoding.default
//        }else{
//            encodingMethod = URLEncoding.default
//        }
        
        print("\(type)"+"/"+"API:"+fullUrl)
        
        //把token 放入header
        var dicToken:[String:String]!
        dicToken = ["tokenId":""]
        
        
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 10
        manager.request(fullUrl, method:method,parameters: parameters,encoding:URLEncoding.default, headers:dicToken)
            .responseString { (response) in
                
                switch response.result {
                    
                case .success:
                    
                    if let value = response.result.value {
                        
                        finished(value)
                    }
                    
                case .failure(let error):
                    
                    print("GetErrorUrl:\(String(describing: response.request))")
                    
                    print("GetError:\(error)")
                }
                
        }
    }
    

    
}


